package com.yc.navy.goods.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.navy.goods.bean.BalanceInfo;
import com.yc.navy.goods.bean.FindByPageInfo;
import com.yc.navy.goods.bean.ProductInfo;
import com.yc.navy.goods.dto.ConstantInfoDTO;
import com.yc.navy.goods.dto.ProductDTODTO;
import com.yc.navy.goods.dto.ProductInfoDTO;
import com.yc.navy.goods.dto.ShopInfoDTOO;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.feign.IUserFeignClient;
import com.yc.navy.goods.service.IBalanceInfoService;
import com.yc.navy.goods.service.IProductInfoService;
import com.yc.navy.goods.util.StringUtil;
import com.yc.navy.goods.vo.ResultVO;
import com.yc.navy.goods.vo.StreamVO;

@RestController
@RequestMapping("/product")
public class ProductInfoController {
	@Autowired
	private IProductInfoService productInfoService;
	
	@Autowired
	private IBalanceInfoService balanceInfoService;
	
	@Autowired
	private RedisTemplate<String,Object> redisTemplate;
	
	@Autowired
	private IUserFeignClient userFeignClient;
	
	@Transactional
	@RequestMapping("/add")
	public ResultVO add(ProductInfo pf,MultipartFile[] photos) {
		String savepath = "";
		File dest = null;
		
		if(photos != null && photos.length > 0 && photos[0].getSize() > 0) {
			String picStr = "";
			try {
				for(MultipartFile f1: photos) {
					savepath = ConstantInfoDTO.UPLOADPATH + "/" + System.currentTimeMillis() + "_" + f1.getOriginalFilename();
					dest = new File(ConstantInfoDTO.basepath,savepath);
					f1.transferTo(dest);
					if("".equals(picStr)) {
						picStr = savepath;
					}else{
						picStr += ";" + savepath;
					}
				}
				
				pf.setPics(picStr);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		int result = productInfoService.add(pf);
		if(result == -1) {
			return new ResultVO(ResultEnum.INCOMPLETEINFORMATION);
		}
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		int result2 = productInfoService.addbalance(pf);
		
		int result3 = balanceInfoService.findbid();
		if(result3 <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		redisTemplate.opsForHash().put("p_"+pf.getPid(), "b_"+result3, pf.getBnum());
		if(result2 <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.SUCCESS);
		
	}
	
	@RequestMapping("/finds")
	public StreamVO finds(HttpSession session,@RequestParam("page") int page ,@RequestParam("limit") int limit) {
		Map<String,Object> map = new HashMap<String,Object>();
		if(!StringUtil.checkNull(page,limit)){
			map.put("page", page);
			map.put("limit",limit);
		}
		
		return productInfoService.findByPageForBack(map);
	}
	
	@RequestMapping("/lists")
	public ResultVO lists() {
		List<ProductDTODTO> list =  productInfoService.lists();
		if(list.isEmpty() || list.size() <=0 ) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/list")
	public ResultVO list(Integer tid) {
		List<ProductDTODTO> list = productInfoService.list(tid);
		if(list.isEmpty() || list.size() <= 0 ) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/findone")
	public ResultVO findone(Integer pid) {
		List<ProductDTODTO> list = productInfoService.findone(pid);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/findlist")
	public ResultVO findlist(Integer cid) {
		List<ProductDTODTO> list = productInfoService.findlist(cid);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@PostMapping("/search")
	public Map<String, Object> find(String pname){
		return productInfoService.find(pname);
	}
	
	@RequestMapping("/findListnow")
	public ResultVO findListnow() {
		List<ProductDTODTO> list = productInfoService.findListnow();
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/productList")
	public ResultVO productList(Integer sid,Integer page, Integer rows) {
		page = (page - 1) * rows;
		FindByPageInfo bypage = new FindByPageInfo();
		bypage.setId(sid);
		bypage.setPage(page);
		bypage.setRows(rows);
		
		List<ProductInfoDTO> list =  productInfoService.productLists(bypage);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
 		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/producttotal")
	public ResultVO producttotal(Integer sid) {
		int result = productInfoService.producttotal(sid);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,result);
	}
	
	@RequestMapping("/productIdList")
	public ResultVO productIdList(Integer sid,Integer page,@RequestParam("pageSize") Integer rows) {
		page = (page - 1) * rows;
		FindByPageInfo bypage = new FindByPageInfo();
		bypage.setId(sid);
		bypage.setPage(page);
		bypage.setRows(rows);
		
		List<ProductInfoDTO> list =  productInfoService.productIdList(bypage);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
 		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@Transactional
	@RequestMapping("/findShopid")
	public ResultVO findShopid(Integer pid) {
		Integer result = productInfoService.findShopid(pid);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		ShopInfoDTOO shop = userFeignClient.Shopone(result);
		if(shop == null ) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,shop);
	}
	
	@RequestMapping("/findShopList")
	public ResultVO findShopList(Integer sid) {
		List<ProductInfo> list = productInfoService.findShopList(sid);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/findBackProductsCount")
	public ResultVO findBackProductsCount() {
		Integer result = productInfoService.findBackProductsCount();
		if(result < 0) {
			return new  ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,result);
	}
	
	@RequestMapping("/findBackProductsList")
	public ResultVO findBackProductsList(Integer page,@RequestParam("pageSize") Integer rows) {
		page = (page - 1) * rows;
		FindByPageInfo bypage = new FindByPageInfo();
		bypage.setPage(page);
		bypage.setRows(rows);
		return productInfoService.findBackProductsList(bypage);
	}
	
	@RequestMapping("/updateStatus")
	public ResultVO updateStatus(ProductInfo product) {
		Integer result = productInfoService.updateStatus(product);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	@Transactional
	@RequestMapping("/updateProduct")
	public ResultVO updateProduct(ProductInfoDTO product,Integer num) {
		Integer bid = balanceInfoService.findBalanceByPid(product);
		Integer result = productInfoService.updateProductInfo(product);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR,1);
		}
		
		Integer result2 = null;
		if(bid == null){
			
			BalanceInfo balance = new BalanceInfo();
			balance.setBcolor(product.getBcolor());
			balance.setBsize(product.getBsize());
			balance.setBnum(product.getBnum());
			balance.setPid(product.getPid());
			result2 = balanceInfoService.add(balance);
			
			if(result2 <= 0) {
				return new ResultVO(ResultEnum.ERROR,2);
			}
			balanceInfoService.delbalanceBnum(product.getBid());
			return new ResultVO(ResultEnum.SUCCESS);
		}else {
			if(bid == product.getBid()) {
				balanceInfoService.updateBalance(product);
			}else {
				
				BalanceInfo balance = new BalanceInfo();
				balance.setBid(bid);
				balance.setBnum(product.getBnum());
				
				result2 = balanceInfoService.addNum(balance);
				if(result2 <= 0) {
					return new ResultVO(ResultEnum.ERROR,3);
				}
				balanceInfoService.delbalanceBnum(product.getBid());
				
			}
			return new ResultVO(ResultEnum.SUCCESS);
		}
	}
}
